﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace unTroublemaker.Conditions
{
    public class InvertCondition : SpecificationItemBase
    {
        public InvertCondition(IEnumerable<ISpecificationItem> items)
        {
            ChildItems = items.ToArray();
            if(ChildItems.Length == 0)
                throw new Exception("InvertCondition requires at least one child item");
        }

        protected override bool RunTest()
        {
            foreach(var item in ChildItems)
                item.Run();

            return ChildItems.All(i => i.Passed == false);
        }

        public override string DisplayName
        {
            get
            {
                if (ChildItems.Length > 1)
                    return string.Format("Check that {0} items does not pass", ChildItems.Length);

                return string.Format("Check that '{0}' fails", ChildItems[0].DisplayName);
            }
        }
    }
}
